#include "lvgl/lvgl.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>

// 修改缓冲区大小为全屏尺寸 (800x480x2 = 768,000 字节)
#define DISP_BUF_SIZE (800 * 480)

// 函数声明
void screen_event_handler(lv_event_t * e);
void show_gif_screen(void);
void show_png_screen(void);
void clear_screen(void);
bool file_exists(const char *path);
void init_fs(void);
extern void lcd_inter_face(void);  // 外部函数声明（在lv_index.c中实现）

// 全局变量
lv_obj_t * gif_obj = NULL;
lv_obj_t * png_obj = NULL;
lv_obj_t * overlay = NULL;




// 屏幕状态
typedef enum {
    SCREEN_GIF,      // 显示GIF
    SCREEN_PNG       // 显示PNG图片
} screen_state_t;

screen_state_t current_screen = SCREEN_GIF;

// 检查文件是否存在
bool file_exists(const char *path) {
    struct stat buffer;
    return (stat(path, &buffer) == 0);
}

// 清屏
void clear_screen(void) {
    if (gif_obj) {
        lv_obj_del(gif_obj);
        gif_obj = NULL;
    }
    if (png_obj) {
        lv_obj_del(png_obj);
        png_obj = NULL;
    }
    if (overlay) {
        lv_obj_del(overlay);
        overlay = NULL;
    }
}

// 屏幕点击事件处理
void screen_event_handler(lv_event_t * e) {
    if (lv_event_get_code(e) == LV_EVENT_CLICKED) {
        if (current_screen == SCREEN_GIF) {
            clear_screen();
            show_png_screen();
            current_screen = SCREEN_PNG;
        } else if (current_screen == SCREEN_PNG) {
            clear_screen();
            // 调用外部函数
            lcd_inter_face();
            current_screen = SCREEN_GIF;
        }
    }
}

// 显示GIF界面
void show_gif_screen(void) {
    lv_obj_t * scr = lv_scr_act();
    
    // 使用S:路径
    gif_obj = lv_gif_create(scr);
    lv_gif_set_src(gif_obj, "S:loading.gif");
    lv_obj_align(gif_obj, LV_ALIGN_CENTER, 0, 0);
    
    // 覆盖层（触摸）
    overlay = lv_obj_create(scr);
    lv_obj_set_size(overlay, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_opa(overlay, LV_OPA_TRANSP, 0);
    lv_obj_add_flag(overlay, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(overlay, screen_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_move_foreground(overlay);
}

// 显示PNG界面
void show_png_screen(void) {
    lv_obj_t * scr = lv_scr_act();
    
    // 使用S:路径
    png_obj = lv_img_create(scr);
    lv_img_set_src(png_obj, "S:quit.png");
    lv_obj_align(png_obj, LV_ALIGN_CENTER, 0, 0);
    
    // 覆盖层(触摸)
    overlay = lv_obj_create(scr);
    lv_obj_set_size(overlay, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_opa(overlay, LV_OPA_TRANSP, 0);
    lv_obj_add_flag(overlay, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(overlay, screen_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_move_foreground(overlay);
}

void init_fs(void) {
    lv_fs_drv_t fs_drv;
    lv_fs_drv_init(&fs_drv);
    
    fs_drv.letter = 'S';
    
    // 强制类型转换解决回调函数指针警告
    fs_drv.open_cb = (void*)lv_fs_open;
    fs_drv.close_cb = (void*)lv_fs_close;
    fs_drv.read_cb = (void*)lv_fs_read;
    fs_drv.write_cb = (void*)lv_fs_write;
    fs_drv.seek_cb = (void*)lv_fs_seek;
    fs_drv.tell_cb = (void*)lv_fs_tell;
    fs_drv.dir_open_cb = (void*)lv_fs_dir_open;
    fs_drv.dir_read_cb = (void*)lv_fs_dir_read;
    fs_drv.dir_close_cb = (void*)lv_fs_dir_close;
    
    lv_fs_drv_register(&fs_drv);
}

int main(void) {
    lv_init();
    init_fs();
    fbdev_init();
    
    // 显示缓冲区 - 修改为全屏尺寸
    static lv_color_t buf[DISP_BUF_SIZE];
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);
    
    // 显示驱动
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &disp_buf;
    disp_drv.flush_cb = fbdev_flush;
    disp_drv.hor_res = 800;
    disp_drv.ver_res = 480;
    lv_disp_drv_register(&disp_drv);
    
    // 输入设备
    evdev_init();
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = evdev_read;
    lv_indev_drv_register(&indev_drv);
    
    // 显示初始GIF界面
    show_gif_screen();
    current_screen = SCREEN_GIF;
    
    // 主循环
    while (1) {
        lv_timer_handler();
        usleep(5000);
    }
    return 0;
}

/* 自定义tick函数 */
uint32_t custom_tick_get(void) {
    static uint64_t start_ms = 0;
    if (start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    return (uint32_t)(now_ms - start_ms);
}

